$(document).ready(function () {
    $("form").validation({icon:true});
    $('.table-fixed-header').fixedHeader();
    window.arr=[];
    window.ExpressNo=0;//全局面单数量
    window.Blance=0;//全局充值金额数
    window.WeightLimit=0;//全局限制重量
    getLetterSumAndMoneySum();
    $(".orderInfoSub").click(function () {
        saveOrderInfo();
    });
    $(".f_orderSub").click(function () {
        submitOrder();
    });
    $(".isLocalCity").change(function(){
        var isChoose = $(".isLocalCity")[0].checked;
        isLocalCity(isChoose);
    });
    $(".temp-order-number").blur(function () {
        var orderNo = $(this).val();
        if(orderNo!=null&&orderNo!=""&&orderNo.length==6){
            getTempOrder(orderNo);
        }
    });
});
function saveOrderInfo() {
        var order = {};
        var user = {};//录入者
        var date;//录入时间
        var orderNo;//订单编号
        var itemName;//产品名称
        var price;//价格
        var weight;//物体重量
        var toName;//收件人
        var cellphone;//电话号码
        var city;//城市
        var area;//区县
        var street;//街道
        var address;//详细地址
        var isChoose;//是否勾选协议
        var isQuick;//是否加急
        var isLocalCity;//是否同城
        orderNo = $(".number").val();
        itemName = $(".item-name").val();
        toName = $(".receiver").val();
        cellphone = $(".tel").val();
        city = $(".city_list option:selected").html();
        area = $(".area_list option:selected").html();
        street = $(".street_list option:selected").html();
        address = $(".address").val();
        price = $(".price").val();
        weight= $(".weight").val();
        isChoose =$(".agreement")[0].checked;
        isQuick = $(".isQuick")[0].checked;
        isLocalCity = $(".isLocalCity")[0].checked;
        if(orderNo==""){
            window.wxc.xcConfirm("订单号不能为空！", window.wxc.xcConfirm.typeEnum.warning);
        }else if(orderNo.length<6){
            window.wxc.xcConfirm("请输入6位订单号！", window.wxc.xcConfirm.typeEnum.warning);
        }
        else if(itemName==""){
            window.wxc.xcConfirm("物品名称不能为空！", window.wxc.xcConfirm.typeEnum.warning);
        }else if(toName==""){
            window.wxc.xcConfirm("收件人不能为空！", window.wxc.xcConfirm.typeEnum.warning);
        }else if(cellphone==""){
            window.wxc.xcConfirm("电话不能为空！", window.wxc.xcConfirm.typeEnum.warning);
        }else if(cellphone.length<11){
            window.wxc.xcConfirm("请输入11位手机号！", window.wxc.xcConfirm.typeEnum.warning);
        }
        else if(city==""){
            window.wxc.xcConfirm("请选择城市！", window.wxc.xcConfirm.typeEnum.warning);
        }else if(area==""){
            window.wxc.xcConfirm("请选择区/县！", window.wxc.xcConfirm.typeEnum.warning);
        }else if(address==""){
            window.wxc.xcConfirm("请选择街道！", window.wxc.xcConfirm.typeEnum.warning);
        }else if(weight==""){
            window.wxc.xcConfirm("物品重量不能为空！", window.wxc.xcConfirm.typeEnum.warning);
        }else if(price==""){
            window.wxc.xcConfirm("物品价格不能为空！", window.wxc.xcConfirm.typeEnum.warning);
        }else  if(isChoose==false){
            window.wxc.xcConfirm("请勾选协议！", window.wxc.xcConfirm.typeEnum.warning);
        }else if(window.ExpressNo<1){//parseFloat(123)
            window.wxc.xcConfirm("剩余面单数不足！", window.wxc.xcConfirm.typeEnum.error);
        }else if(window.Blance<Math.ceil(parseFloat(weight))-window.WeightLimit){//parseFloat(123)
            window.wxc.xcConfirm("剩余金额不足！", window.wxc.xcConfirm.typeEnum.error);
        }
        else {
            $.getJSON("../order_deadline.json",{"destination":city},function (data) {
                if(data.success){
                    // window.wxc.xcConfirm(data.msg, window.wxc.xcConfirm.typeEnum.success);
                }else {
                    window.wxc.xcConfirm(data.msg, window.wxc.xcConfirm.typeEnum.error);
                }
                $.getJSON("../order_isAgain.json",{"orderNo":orderNo},function (data) {
                    if(data.success){
                        var quick;
                        if(isQuick==true){
                            quick="加急件"
                        }else{
                            quick="";
                        }
                        order.orderNo = orderNo;
                        order.itemName=itemName;
                        order.toName=toName;
                        order.cellphone=cellphone;
                        order.city=city;
                        order.area=area;
                        order.street=street;
                        order.address=address;
                        order.price=price;
                        order.isQuick=isQuick;
                        order.isLocalCity=isLocalCity;
                        order.weight=weight;
                        var flag = true;
                        $(".table-add-order tr:gt(0)").each(function (i,value) {
                            var No = $(this).children(":nth-child(3)").html();
                            if(No==orderNo){
                                window.wxc.xcConfirm("请勿录入重复订单", window.wxc.xcConfirm.typeEnum.error);
                                flag = false;
                                return false;
                            }

                        });
                        if(flag){
                            if(window.ExpressNo<1){
                                window.wxc.xcConfirm("剩余面单数不足！", window.wxc.xcConfirm.typeEnum.error);
                            }else{
                                $(".table-add-order").append("<tr>"+
                                    "<td>"+quick+"</td>" +
                                    "<td><button class='del btn btn-primary btn-sm btn-table'>删除</button></td>" +
                                    "<td>"+orderNo+"</td>" +
                                    "<td>"+itemName+"</td>" +
                                    "<td>"+toName+"</td>" +
                                    "<td>"+cellphone+"</td>" +
                                    "<td>"+city+area+street+address+"</td>" +
                                    "<td>"+price+"</td>" +
                                    "<td hidden='hidden'><input  value='"+weight+"'></td>"+
                                    "</tr>");
                                $(".del").unbind("click");
                                $(".del").click(function () {
                                    $(this).parent().parent().remove();
                                    window.ExpressNo++;
                                    window.Blance=window.Blance+Math.ceil( parseInt($(this).parent().parent().find("input").val()-window.WeightLimit));
                                    var delOrderNo = $(this).parent().next("td")[0].innerHTML;
                                    if(delOrderNo!=""){
                                        for(var i=0;i<window.arr.length;i++){
                                            if(window.arr[i].orderNo==delOrderNo){
                                                window.arr.splice(i,1);
                                            }
                                        }
                                    }
                                });
                                $(".number").val("");
                                $(".temp-order-number").val("");
                                $(".item-name").val("");
                                $(".receiver").val("");
                                $(".tel").val("");
                                $("[name='city_list']").val("");
                                $("[name='area_list']").val("");
                                $(".address").val("");
                                $(".price").val("");
                                $(".weight").val("");
                                $(".agreement")[0].checked=false;
                                $(".isQuick")[0].checked=false;
                                $(".isLocalCity")[0].checked=false;
                                $(".city_list").val("");
                                $(".area_list").val("");
                                $(".street_list").val("");
                                $(".city_list").removeAttr("disabled");
                                getAddress(0,"city_list");
                                window.arr.push(order);
                                window.ExpressNo--;
                                //if(weight>window.WeightLimit){
                                window.Blance=window.Blance-Math.ceil(parseFloat(weight)-window.WeightLimit);
                                //}
                            }
                        }
                    }else{
                        window.wxc.xcConfirm(data.msg, window.wxc.xcConfirm.typeEnum.error);
                    }
                });
            });
        }
}
function submitOrder() {
    if(window.arr==null|| window.arr.length==0){
        window.wxc.xcConfirm("不能录入空订单！", window.wxc.xcConfirm.typeEnum.error);
    }else {
        $.ajax({
            url:"../order_input.json",
            type:"POST",
            data:JSON.stringify(window.arr),
            contentType:"application/json; charset=utf-8",
            dataType:"json",
            success: function(msg){
                if(msg.success==true){
                    $("#tableOne tr:gt(1)").remove();
                    getBranchStationForLetter();
                    getCounterForLetter();
                    getLetterSumAndMoneySum();
                    getexrpessNoandBalance();
                    window.arr =[];
                    window.wxc.xcConfirm(msg.msg, window.wxc.xcConfirm.typeEnum.success);
                }else {
                    window.wxc.xcConfirm(msg.msg, window.wxc.xcConfirm.typeEnum.error);
                }
            }
        })
    }
}
function isLocalCity(isChoose){
    $.getJSON("../user_me.json",function(data){
       if(data.success){
           var city = data.user.city;
           if(isChoose){
               $(".city_list option:contains('"+city+"'):eq(0)").removeAttr("disabled");
               $(".city_list option:contains('"+city+"'):eq(0)")[0].selected=false;
               $(".area_list ").val("");
               $(".street_list ").val("");
               $(".area_list option").remove();
               $(".street_list option").remove();
               $(".city_list option:contains('"+city+"'):eq(0)")[0].selected=true;
               $(".city_list").attr("disabled",true);
               var parentId = $(".city_list option:selected").val();
               getAddress(parentId,"area_list");
           }else{
               $(".area_list ").val("");
               $(".street_list ").val("");
               $(".address").val("");
               $(".city_list").removeAttr("disabled").val("");
               $(".city_list option:contains('"+city+"'):eq(0)").attr("disabled",true);
           }
       }else{
           console.log("getUserisError");
       }
    });
};
function getLetterSumAndMoneySum() {
    $.getJSON("../order_check.json",function (msg) {
        if(msg.success==true){
            window.ExpressNo=msg.ExpressNo;
            window.Blance=msg.Blance;
            window.WeightLimit=msg.WeightLimit;
        }else{
            window.ExpressNo=0;
            window.Blance=0;
            window.WeightLimit=0;
        }
    })
}
function getTempOrder(orderNo) {
    $.getJSON("../TempOrder_queryByNo.json",{"orderNo":orderNo},function (data) {
        if(data.success){
            $(".city_list").attr("disabled",false);
            $(".name").val(data.TempOrder.itemName);
            $(".receiver").val(data.TempOrder.toName);
            $(".tel").val(data.TempOrder.cellphone);
            $(".weight").val(data.TempOrder.weight);
            $(".city_list option:contains('"+data.TempOrder.city+"'):eq(0)")[0].selected=true;
            getAddressForTempOrder($("#city_list option:selected").val(),"area_list",data.TempOrder.area,data.TempOrder.street);
            $(".address").val(data.TempOrder.address);
            $(".price").val(data.TempOrder.price);
            $(".isQuick")[0].checked=data.TempOrder.isQuick;
            $(".isLocalCity")[0].checked=data.TempOrder.isLocalCity;
            if(data.TempOrder.isLocalCity){
                $(".city_list").attr("disabled",true);
            }
        }else {

        }
    })
}
function getAddressForTempOrder(parentId,id,areaKey,streetKey){
    $("." + id).html("<option></option>");
    var parentId =parentId;
    $.getJSON("../address_query.json",{parentId : parentId},function(data){
        if(data.success && data.address != null){
            $.each(data.address,function(index,item){
                var option = "<option value='"+item.id+"'>"+item.name+"</option>";
                $("." + id).append(option);
            });
            if(id=="area_list"){
                $(".area_list option:contains('"+areaKey+"'):eq(0)")[0].selected=true;
                getAddressForTempOrder($("#area_list option:selected").val(),"street_list",areaKey,streetKey);
            }else if(id=="street_list"){
                $(".street_list option:contains('"+streetKey+"'):eq(0)")[0].selected=true;
            }
        }

    });
}
